﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.Micro;
using Agnes.Core;
using Agnes.Libaray;
using System.Windows;
using System.IO;
using Microsoft.Win32;
using System.Windows.Forms;
using ICSharpCode.Core;

namespace Agnes.Mainframe
{
    /// <summary>
    /// New Sln View Model
    /// </summary>
    public class NewSlnViewModel
        : ScreenBase<AgnesSln>
    {
        /// <summary>
        /// Create View
        /// </summary>
        /// <returns></returns>
        protected override object CreateView()
        {
            return new NewSlnView();
        }

        public NewSlnViewModel(AgnesSln newSln)
        {
            Model = newSln;
            CanCreateNewSln = Model.IsValid;
            Model.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Model_PropertyChanged);
        }

        void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            CanCreateNewSln = Model.IsValid;
        }

        #region Binding

        private bool _canCreateNewSln = false;
        /// <summary>
        /// CanCreateNewSln
        /// </summary>
        public bool CanCreateNewSln
        {
            get
            { return _canCreateNewSln; }
            set
            {
                _canCreateNewSln = value;
                NotifyOfPropertyChange(() => CanCreateNewSln);
            }
        }

        public void CreateNewSln()
        {
            if (!File.Exists(Model.FullName))
                Model.Package();
            else
            {
                if (System.Windows.MessageBox.Show(Model.SlnName + "已经存在,是否覆盖该文件?", "警告",
                    MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                {
                    Model.Package();
                }
            }
        }

        public void Browser()
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.SelectedPath =PropertyService.Get("DefaultSlnDir");
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                PropertyService.Set("DefaultSlnDir", fbd.SelectedPath);
                Model.LocatedDir = fbd.SelectedPath;
            }
        }


        #endregion
    }
}
